package nlp.blcu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.mira.lucene.analysis.IK_CAnalyzer;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Main extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField index_Inp;
	private JTextField input;
	private JButton load_but;
	private JTextField jTextField1;
	private JTextArea result_lable;
	private JButton search_but;
	private JTextField input_Lable;
	private JTextField index_Lable;

	
	
	private IndexReader reader;
	private Analyzer analyzer = new IK_CAnalyzer();
	private Searcher searcher;
	private QueryParser parser;
	private Query query;
	private TopDocs hits;
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Main inst = new Main();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public Main() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("IdomSearch");
			getContentPane().setLayout(null);
			this.setPreferredSize(new java.awt.Dimension(800, 500));
			{
				index_Inp = new JTextField();
				getContentPane().add(index_Inp, "North");
				index_Inp.setBounds(114, 25, 472, 29);
				index_Inp.setSize(473, 30);
			}
			{
				index_Lable = new JTextField();
				getContentPane().add(index_Lable);
				index_Lable.setText(" Index Path :");
				index_Lable.setBounds(17, 25, 84, 29);
				index_Lable.setEditable(false);
				index_Lable.setBackground(new java.awt.Color(192,192,192));
				index_Lable.setSize(84, 30);
			}
			{
				input_Lable = new JTextField();
				getContentPane().add(input_Lable);
				input_Lable.setText(" Search For :");
				input_Lable.setBounds(17, 84, 84, 28);
				input_Lable.setSize(84, 30);
				input_Lable.setBackground(new java.awt.Color(192,192,192));
				input_Lable.setEditable(false);
			}
			{
				input = new JTextField();
				getContentPane().add(input);
				input.setBounds(113, 84, 473, 28);
				input.setSize(473, 30);
				input.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						inputMouseClicked(evt);
					}
				});
			}
			{
				load_but = new JButton();
				getContentPane().add(load_but);
				load_but.setText("LoadIndex");
				load_but.setBounds(626, 25, 104, 30);
				load_but.setBackground(new java.awt.Color(0,0,255));
				load_but.setForeground(new java.awt.Color(255,255,255));
				load_but.setPreferredSize(new java.awt.Dimension(104, 30));
				load_but.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						load_butMouseClicked(evt);
					}
				});
			}
			{
				search_but = new JButton();
				getContentPane().add(search_but);
				search_but.setText("Search");
				search_but.setBounds(626, 84, 104, 30);
				search_but.setBackground(new java.awt.Color(0,0,255));
				search_but.setForeground(new java.awt.Color(255,255,255));
				search_but.setPreferredSize(new java.awt.Dimension(104, 30));
				search_but.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							search_butMouseClicked(evt);
						} catch (CorruptIndexException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				result_lable = new JTextArea();
				getContentPane().add(result_lable);
				result_lable.setBounds(115, 142, 473, 282);
				result_lable.setBackground(new java.awt.Color(201,244,157));
			}
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1);
				jTextField1.setText("    Result :");
				jTextField1.setEditable(false);
				jTextField1.setBackground(new java.awt.Color(192,192,192));
				jTextField1.setBounds(17, 142, 84, 30);
			}
			pack();
			this.setSize(784, 486);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void load_butMouseClicked(MouseEvent evt) {
		initIndex();
		//TODO add your code for load_but.mouseClicked
	}
	
	private void search_butMouseClicked(MouseEvent evt) throws CorruptIndexException, IOException {
		this.result_lable.setText("");
		search();
		//TODO add your code for search_but.mouseClicked
	}
	
	private void inputMouseClicked(MouseEvent evt) {
		input.setText("");
		//TODO add your code for input.mouseClicked
	}
	
	private void search() throws CorruptIndexException, IOException{
		try {
			query = parser.parse(this.input.getText());
			hits = searcher.search(query,20);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i < hits.scoreDocs.length; i++){
        	ScoreDoc scoreDoc = hits.scoreDocs[i];
        	Document doc = searcher.doc(scoreDoc.doc);
        	String filePath;
        	filePath = doc.get("path");
        	int pos = filePath.lastIndexOf("\\");
        	int last = filePath.lastIndexOf(".");
        	this.result_lable.setText(this.result_lable.getText()+filePath.substring(pos+1,last)+"\n");
		}
	}
	private void initIndex(){
		if (this.index_Inp.getText().length()==0) {
			return;
		}
		try {
			reader = IndexReader.open(FSDirectory.open(new File(this.index_Inp.getText())), true);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // only searching, so read-only=true
	    this.searcher = new IndexSearcher(reader);
	    this.parser = new QueryParser("contents", analyzer);
	}
}
